<template>
  <s-layout class="wallet-wrap" title="我的钱包" :bgStyle="template.style?.background"
            :navbarStyle="template.style?.navbar">
    <!-- 钱包卡片 -->
    <view class="header-box ss-flex ss-row-center ss-col-center">
      <view class="card-box">
        <view class="top">
          <view class="card-head ss-flex ss-col-center">
            <view class="card-title ss-m-r-10">钱包</view>
          </view>
          <view class="ss-flex ss-row-between ss-col-center ss-m-t-64">
            <view class="money-num">{{ userInfo.money }}</view>
            <button class="ss-reset-button topup-btn" @tap="sheep.$router.go('/pages/pay/recharge')">
              充值
            </button>
          </view>
        </view>
        <view class="center">
          <view class="left">转账
            <text class="_icon-forward right-forwrad-icon"></text>
          </view>
          <view class="right" @tap="sheep.$router.go('/pages/pay/withdraw')">提现
            <text class="_icon-forward right-forwrad-icon"></text>
          </view>
        </view>
        <view class="packet">
          <view class="packet-left">
            <image class="packet-left-image" src="/static/img/packet.png" />
            红包
          </view>
          <view class="packet-right">99.00</view>
        </view>
      </view>
    </view>

    <su-sticky>
      <!-- 统计 -->
      <my-tabs
        :list="tabMaps"
        @change="onChange"
        :scrollable="false"
        :current="state.currentTab"
      ></my-tabs>
    </su-sticky>
    <s-empty v-if="state.pagination.total === 0" text="暂无数据" icon="/static/data-empty.png" />
    <view class="list" v-if="state.pagination.total > 0">
      <wallet v-for="item in state.pagination.data"
              :key="item.id" :data="item" v-show="state.currentTab==0"></wallet>
      <packet v-for="item in state.pagination.data"
              :key="item.id" :data="item" v-show="state.currentTab==1"></packet>
      <transfer v-for="item in state.pagination.data"
                :key="item.id" :data="item" v-show="state.currentTab==2"></transfer>
      <withdraw v-for="item in state.pagination.data"
                :key="item.id" :data="item" v-show="state.currentTab==3"></withdraw>
    </view>
    <uni-load-more
      v-if="state.pagination.total > 0"
      :status="state.loadStatus"
      :content-text="{
        contentdown: '上拉加载更多',
      }"
    />
  </s-layout>
</template>

<script setup>
  import { computed, reactive } from 'vue';
  import { onLoad, onReachBottom } from '@dcloudio/uni-app';
  import packet from './components/packet.vue';
  import transfer from './components/transfer.vue';
  import withdraw from './components/withdraw.vue';
  import wallet from './components/wallet.vue';
  import sheep from '@/sheep';
  import dayjs from 'dayjs';
  import _ from 'lodash';

  const template = computed(() => sheep.$store('app').template?.home);
  const headerBg = sheep.$url.css('/assets/addons/shopro/uniapp/user/wallet_card_bg.png');

  const pagination = {
    data: [],
    current_page: 1,
    total: 1,
    last_page: 1,
    expense: 0,
    income: 0,
  };
  // 数据
  const state = reactive({
    showMoney: false,
    date: [],
    currentTab: 0,
    pagination,
    loadStatus: '',
    today: '',
  });

  const tabMaps = [
    {
      name: '钱包明细',
      value: 'wallet',
    },
    {
      name: '红包明细',
      value: 'packet',
    },
    {
      name: '转账明细',
      value: 'transfer',
    }, {
      name: '提现明细',
      value: 'withdraw',
    },
  ];
  const userInfo = computed(() => sheep.$store('user').userInfo);

  const dateFilterText = computed(() => {
    if (state.date[0] === state.date[1]) {
      return state.date[0];
    } else {
      return state.date.join('~');
    }
  });

  async function getLogList(page = 1, list_rows = 8) {
    state.loadStatus = 'loading';
    let res = await sheep.$api.user.wallet.log({
      type: 'money',
      tab: tabMaps[state.currentTab].value,
      list_rows,
      page,
      date: appendTimeHMS(state.date),
    });
    if (res.code === 1) {
      let list = _.concat(state.pagination.data, res.data.list.data);
      state.pagination = {
        ...res.data.list,
        data: list,
        income: res.data.income,
        expense: res.data.expense,
      };
      if (state.pagination.current_page < state.pagination.last_page) {
        state.loadStatus = 'more';
      } else {
        state.loadStatus = 'noMore';
      }
    }
  }

  onLoad(async (options) => {
    state.today = dayjs().format('YYYY-MM-DD');
    state.date = [state.today, state.today];
    getLogList();
  });

  function onChange(e) {
    state.pagination = pagination;
    state.currentTab = e.index;
    getLogList();
  }

  function onChangeTime(e) {
    state.date[0] = e[0];
    state.date[1] = e[e.length - 1];
    state.pagination = pagination;
    getLogList();
  }

  function appendTimeHMS(arr) {
    return [arr[0] + ' 00:00:00', arr[1] + ' 23:59:59'];
  }

  onReachBottom(() => {
    if (state.loadStatus !== 'noMore') {
      getLogList(state.pagination.current_page + 1);
    }
  });
</script>

<style lang="scss" scoped>
  .list {
    margin-top: 30rpx;
  }

  :deep() {
    .red {
      color: #bb2b1e !important;
    }

    .blue {
      color: #4c84c9 !important;
    }

    .orange {
      color: #a66033 !important;
    }

    .green {
      color: #518f73 !important;
    }
  }

  // 钱包
  .header-box {
    padding: 30rpx;

    .card-box {
      width: 100%;
      min-height: 300rpx;
      background-size: 100% 100%;
      border-radius: 30rpx;
      overflow: hidden;
      position: relative;
      z-index: 1;
      border: 2px solid #505050;
      box-sizing: border-box;
      height: 460rpx;
      background-color: #1c1b1c;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .top {
        padding: 40rpx 40rpx 0 40rpx;
      }

      .center {
        display: flex;
        font-size: 28rpx;
        text-align: center;
        width: 100%;
        padding: 20rpx 40rpx;

        .left {
          width: 100%;
          flex: 1;
          color: #F17126;
          border-right: 1px solid #505050;
        }

        .right {
          width: 100%;
          flex: 1;
          color: #13C093;
        }
      }

      .packet {
        padding: 40rpx;
        display: flex;
        justify-content: space-between;
        border-radius: 20rpx;
        border: 2px solid #505050;
        border-bottom: none;
        border-left: none;
        border-right: none;
        color: #fff;

        .packet-left {
          display: flex;
          align-items: center;
        }

        .packet-left-image {
          width: 38rpx;
          height: 44rpx;
          margin-right: 10rpx;
        }
      }

      .card-head {
        color: $white;
        font-size: 30rpx;
      }

      .ss-eye-icon {
        font-size: 40rpx;
        color: $white;
      }

      .money-num {
        font-size: 70rpx;
        line-height: 70rpx;
        font-weight: 500;
        color: $white;
      }

      .reduce-num {
        font-size: 26rpx;
        font-weight: 400;
        color: $white;
      }

      .topup-btn {
        line-height: 46rpx;
        font-size: 26rpx;
        font-weight: 500;
        width: 90rpx;
        height: 46rpx;
        background: linear-gradient(90deg, #6F6FF1, #7071F0);
        color: $white;
        border-radius: 10rpx;
      }
    }
  }

  // 筛选
  .filter-box {
    height: 114rpx;

    .total-box {
      font-size: 24rpx;
      font-weight: 500;
      color: $dark-9;
    }

    .date-btn {
      background-color: $white;
      line-height: 54rpx;
      border-radius: 27rpx;
      padding: 0 20rpx;
      font-size: 24rpx;
      font-weight: 500;
      color: $dark-6;

      .ss-seldate-icon {
        font-size: 50rpx;
        color: $dark-9;
      }
    }
  }

  .tabs-box {
    background: $white;
    border-bottom: 2rpx solid #eeeeee;
  }

  .u-tabs {
    background-color: transparent;
    border: none;
  }

  // tab
  .wallet-tab-card {
    .tab-item {
      height: 80rpx;
      position: relative;

      .tab-title {
        font-size: 30rpx;
      }

      .cur-tab-title {
        font-weight: $font-weight-bold;
      }

      .tab-line {
        width: 60rpx;
        height: 6rpx;
        border-radius: 6rpx;
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 2rpx;
        background-color: var(--ui-BG-Main);
      }
    }
  }

  // 钱包记录
  .wallet-list {
    padding: 30rpx;
    background-color: #ffff;

    .head-img {
      width: 70rpx;
      height: 70rpx;
      border-radius: 50%;
      background: $gray-c;
    }

    .list-content {
      justify-content: space-between;
      align-items: flex-start;
      flex: 1;

      .title {
        font-size: 28rpx;
        color: $dark-3;
        width: 400rpx;
      }

      .time {
        color: $gray-c;
        font-size: 22rpx;
      }
    }

    .money {
      font-size: 28rpx;
      font-weight: bold;
      font-family: OPPOSANS;

      .add {
        color: var(--ui-BG-Main);
      }

      .minus {
        color: $dark-3;
      }
    }
  }
</style>
